import java.io.*;
import java.util.*;

public class GenerateJavaScript {

 
   
    private static String srcDir     = "C:/myProjects/apache2/htdocs";
    private static String distDir    = "C:/myServers/ApacheGroup/Apache2/htdocs";
    private static String currentDir = null;
    private static String systemDir  = null; 
    private static String htmlDir    = null; 
    private static String jspDir     = null; 
    private static Vector proVector = new Vector(); 

    public static void main( String args[] )
    {

	GenerateJavaScript maker = new GenerateJavaScript();

	if (args.length ==0) {
	    System.out.println("==============================================================");
	    System.out.println("[Enter subdir of C:/myProjects/apache2/htdocs/]");
	    System.out.println("==============================================================");
	    System.exit(0);
	}
	else {
	    File f_dir = new File (srcDir+"/"+args[0]);
	    if (!f_dir.isDirectory()) {
		System.out.println((srcDir+"/"+args[0])+" is not a directory!");
		System.exit(0);
	    }
	}

	maker.setCurrentDir (args[0]);
	maker.loadLeftBarProfile();
	maker.generateJavaScriptFile("sideBarStateBody.js");
    }

    private void loadLeftBarProfile() {

	File check_profile = new File ((srcDir+"/"+currentDir), "_profile.txt");
	if (check_profile.exists()) {
	    ProfileLoader loader = new ProfileLoader();
	    loader.setFileName(srcDir+"/"+currentDir+"/_profile.txt");
	    loader.loadProfile();
	    proVector = loader.getVector();
	}
	else {
	    System.out.println("No profile for this directory");
	}
    }

    private void generateJavaScriptFile(String jsfile)
    {
	
	File file_name = new File (jspDir, jsfile);

	try {

	    if (file_name.exists()) {
		System.out.println("deleting old "+jsfile+" file .....");
		if(file_name.delete()) {
		    System.out.println("Old "+file_name.getName()+" file has been deleted");
		}
	    }
	   
	    RandomAccessFile output_js_file = new RandomAccessFile( file_name, "rw" );
	    /*
	    if ((file_name.getName()).equals("setCookie.js")) {
		output_js_file.writeBytes("function setCurrState(setting) {");
		output_js_file.writeBytes("document.cookie = \"currState=\" + escape(setting);");
		output_js_file.writeBytes("currState = setting};");	
	    }
	    */

	    if ((file_name.getName()).equals("sideBarStateBody.js")) {

		/**
		 * add user profile into side bar
		 **/

		int i = 1;

		if (proVector != null) {
		    String    url_name   = null;
		    String    url_adds   = null;
		    
		    try {

			/**
			 * add profile links
			 */

			output_js_file.writeBytes("db["+ i +"]  = new dbRecord(true,\"<font size=3>");
			output_js_file.writeBytes("TOPLINKS</font>\", \"\",0,\"\",true);");
			i++;

			Enumeration enum = proVector.elements();  
			while (enum.hasMoreElements()) {
			    url_name = (String)enum.nextElement();
			    url_adds = (String)enum.nextElement();
			    output_js_file.writeBytes("db["+i+"]= new dbRecord(false,\"<font size=2>");
			    output_js_file.writeBytes(url_name.toUpperCase()+"</font>\",");
			    output_js_file.writeBytes("\""+url_adds+"\",1,\"\",true);");
			    i++;
			}
		    }
		    catch ( IOException io ) {
		    }
		} // if (profileHsTable != null)
		else {
		    System.out.println("No js profile for");
		}

		/**
		 * add user directories into side bar
		 **/

		String   sub_dir       = null;
		String[] file_list     = null;
		String[] sub_file_list = null;

		file_list = getDirList(srcDir+"/"+currentDir);

		for (int n = 0; n < file_list.length; n++) {
		    System.out.println("file_list["+n+"]"+file_list[n]);
		    File f_file_list = new File ((srcDir+"/"+currentDir), file_list[n]);

		    if (f_file_list.isDirectory()) {
			sub_dir = file_list[n];
			//System.out.println("subDir="+sub_dir);

			if(!sub_dir.startsWith("_")) {
			    sub_file_list = getDirList(srcDir+"/"+currentDir+"/"+sub_dir);
			    /**
			     * add sub directory name into side bar
			     */

			    try {
				output_js_file.writeBytes("db["+i+"]= new dbRecord(true,\"");
				output_js_file.writeBytes(sub_dir.toUpperCase() +"\", \"\",0,\"\",true);");
				i++;
			    }
			    catch ( IOException io ) {
			    }

			    /**
			     * add file for sub directory
			     */

			    File f_sub_file_list;
			    String sub_string = null;
			    for(int j =0; j < sub_file_list.length; j++) {
				f_sub_file_list = new File ((srcDir+"/"+currentDir+"/"+sub_dir), sub_file_list[j]);
				
				if (f_sub_file_list.isFile()) {

				    try {
					String temp =f_sub_file_list.getName();
					sub_string = (temp).substring(0,temp.indexOf("."));
					output_js_file.writeBytes("db["+i+"]= new dbRecord(false,");
					output_js_file.writeBytes("\""+sub_string.toUpperCase()+"\",");
					output_js_file.writeBytes("\""+currentDir+"/"+sub_dir+"/"+
								  temp+"\",1,\"\",true);");
					i++;
				    }
				    catch ( IOException io ) {
				    }
				}
			    }    
			}
		    }
		}
	    }
	    
	    closeFile(output_js_file);
	}
	catch ( IOException e ) {
	}
    }

    private void closeFile(RandomAccessFile raf) 
    {
	try {
	    raf.close();
	}
	catch( IOException ex ) {
	    System.exit( 1 );
	}
    }

    public String[] getDirList(String dir)
    {
	File dirName = new File (dir);
	return(dirName.list());
    }

    public void setCurrentDir (String current_dir)
    {
	currentDir = current_dir;

	systemDir  = distDir +"/"+ currentDir +"/_system";
	File f1 = new File (systemDir);
	f1.mkdirs();
	jspDir     = systemDir + "/javaScript";
	File f3 = new File (jspDir);
	f3.mkdirs();
    }

    public String getCurrentDir()
    {
	return currentDir;
    }
}



